﻿Public Class frmTimKiemNangCao

    Private Sub frmTimKiemNangCao_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbxLoaiHang.DataSource = TapHoaDAO.TatCaLoaiHang()
        cbxLoaiHang.DisplayMember = "lh_Loai"
        cbxLoaiHang.ValueMember = "lh_ID"

        cbxNhaCungCap.DataSource = TapHoaDAO.TatCaNhaCungCap()
        cbxNhaCungCap.DisplayMember = "ncc_Ten"
        cbxNhaCungCap.ValueMember = "ncc_ID"

        cbxXuatXu.DataSource = TapHoaDAO.TatCaXuatXu()
        cbxXuatXu.DisplayMember = "xx_NoiXuatXu"
        cbxXuatXu.ValueMember = "xx_ID"

        cbxGia.SelectedIndex = 0
        cbxSoLuong.SelectedIndex = 0

    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        Dim th As TapHoa = New TapHoa()
        th.m_strSoSanhGia = cbxGia.SelectedItem
        th.m_strSoSanhSoLuong = cbxSoLuong.SelectedItem
        th.m_intGia = Integer.Parse(txtGia.Text.ToString())
        th.m_intSoLuong = Integer.Parse(txtSoLuong.Text.ToString())
        th.m_intIDLoaiHang = cbxLoaiHang.SelectedValue
        th.m_intIDNhaCungCap = cbxNhaCungCap.SelectedValue
        th.m_intIDXuatXu = cbxXuatXu.SelectedValue
        If (TapHoaDAO.TimKiemSanPhamTheoTieuChi(th).Rows.Count > 0) Then
            lvlDanhSachSanPham.Items.Clear()
            Dim dshinhanh As ImageList = New ImageList()
            Dim dt As DataTable = TapHoaDAO.TimKiemSanPhamTheoTieuChi(th)
            Dim i As Integer = 0
            Dim lv As DataRow
            For Each lv In dt.Rows
                Dim hinhanh As Image = Image.FromFile(lv("hh_HinhAnh").ToString())
                imgSanPham.Images.Add(hinhanh)
                lvlDanhSachSanPham.LargeImageList = imgSanPham
                Dim bk As ListViewItem = New ListViewItem(lv("hh_TenHang").ToString(), i)
                i = i + 1
                lvlDanhSachSanPham.Items.Add(bk)
            Next
        Else
            MessageBox.Show("Không tìm thấy sản phẩm ")
        End If
    End Sub
    Private Sub lvlDanhSachSanPham_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvlDanhSachSanPham.DoubleClick
        Dim frm As frmThongTinSanPhamTK = New frmThongTinSanPhamTK()
        Dim i As Integer = 0
        While i < lvlDanhSachSanPham.Items.Count
            If (lvlDanhSachSanPham.Items(i).Selected = True) Then
                frm.Tag = frmQuanLyTapHoa.lvlDanhSachMua
                lbTimKiemNangCao.Text = lvlDanhSachSanPham.Items(i).Text
                frm.Show()
                frm.lbTenSanPham.Text = lvlDanhSachSanPham.Items(i).Text
                Dim th As TapHoa = New TapHoa()
                th.m_strTenHangHoa = lvlDanhSachSanPham.Items(i).Text
                Dim dt As DataTable = TapHoaDAO.LayThongTinMotSanPham(th)
                Dim lv As DataRow
                For Each lv In dt.Rows
                    frm.lbGia.Text = lv("hh_Gia").ToString()
                    frm.lbDonVi.Text = lv("dvt_Loai").ToString()
                    frm.lbSoLuong.Text = lv("hh_SoLuong").ToString()
                    frm.lbNhaCungCap.Text = lv("ncc_Ten").ToString()
                    frm.lbXuatXu.Text = lv("xx_NoiXuatXu").ToString()
                    frm.lbLoaiHang.Text = lv("lh_Loai").ToString()
                    frm.lbViTri.Text = lv("vt_NoiVT").ToString()
                    frm.picDaiDien.ImageLocation = lv("hh_HinhAnh").ToString()
                Next
            End If
            i = i + 1
        End While
    End Sub
End Class